package com.tonghe.service.impl;

import com.tonghe.constants.SystemConstants;
import com.tonghe.domain.ResponseResult;
import com.tonghe.domain.entity.LoginUser;
import com.tonghe.domain.entity.User;
import com.tonghe.service.BlogLoginService;
import com.tonghe.utils.BeanCopyUtils;
import com.tonghe.utils.JwtUtil;
import com.tonghe.utils.RedisCache;
import com.tonghe.vo.BlogUserLoginVo;
import com.tonghe.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Date:2022/2/20- 02- 20- 21:44
 * @author: tonghe
 **/
@Service
public class BlogLoginServiceImpl implements BlogLoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;


    /**
    * @description: 登陆调用此接口
    * @param： [user]
    * @author: TongHe
    * @date: 2022/3/17 17:55
    */
    @Override
    public ResponseResult login(User user) {
        // 详见 Authentication 的实现类
        // 采用 UsernamePasswordAuthenticationToken 方式登陆
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);

        // 判断是否认证通过 没通过则为空
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("用户名或密码错误");
        }

        // 获取 用户详情，看 Authentication 源码
        LoginUser loginUser = (LoginUser)authenticate.getPrincipal();
        // 获取用户id
        String  userId = loginUser.getUser().getId().toString();
        // 根据用户id 生成jwt
        String jwt = JwtUtil.createJWT(userId);
        //把 “用户信息” 存入redis
        redisCache.setCacheObject(SystemConstants.REDIS_USER + userId, loginUser);
        // 把token和userInfo封装返回
        // 把user转换成userInfo
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(loginUser.getUser(), UserInfoVo.class);
        BlogUserLoginVo vo = new BlogUserLoginVo(jwt, userInfoVo);
        return ResponseResult.okResult(vo);
    }
    /**
    * @description: 退出登录
    * @author: TongHe
    * @date: 2022/3/17 17:56
    */
    @Override
    public ResponseResult logout() {

        // 从 SecurityContextHolder 获取token 解析获取 userId from JwtAuthenticationTokenFilter
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        // 获取userId
        Long userId = loginUser.getUser().getId();
        // 移除 redis 中的用户信息
        redisCache.deleteObject(SystemConstants.REDIS_USER + userId);
        //返回封装对象null
        return ResponseResult.okResult();
    }




}
